using System;
using System.Diagnostics.Contracts;
using System.Globalization;

namespace OpenWaves
{
    public partial class FileUrl:Url
    {
        public FileUrl(Uri uri) : base(uri)
        {
            Contract.Requires<ArgumentNullException>(uri != null);
            Contract.Requires<ArgumentException>(uri.IsAbsoluteUri);
            Contract.Requires<ArgumentException>(uri.Scheme == Uri.UriSchemeFile);

            this.path = UrlPath.Parse(this.Uri.AbsolutePath);
            this.query = UrlQueryCollection.Parse(this.Uri.Query);
        }

#if !SILVERLIGHT
        [NonSerialized]
#endif
        private readonly UrlPath path;
#if !SILVERLIGHT
        [NonSerialized]
#endif
        private readonly UrlQueryCollection query;

        public override UrlPath Path
        {
            get { return this.path; }
        }

        public override UrlQueryCollection Query
        {
            get { return this.query; }
        }

        public override string Fragment
        {
            get { return this.Uri.Fragment; }
        }

        public string Scheme
        {
            get { return this.Uri.Scheme; }
        }

        public override AbsoluteUrl ResolveAgainst(AbsoluteUrl baseUrl)
        {
            throw new NotSupportedException();
        }

        protected override string ToUrlString()
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", Uri.UriSchemeFile, Uri.SchemeDelimiter, Url.UnifySpaceEncoding(this.Uri.GetComponents(UriComponents.PathAndQuery, UriFormat.UriEscaped)));
        }

        public override string ToUnescapedString()
        {
            return string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", Uri.UriSchemeFile, Uri.SchemeDelimiter, this.Uri.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped));
        }

        protected override UrlBuilder CreateBuilder()
        {
            return FileUrlBuilder.From(this);
        }
    }
}